﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using ArbolandiaBL;

public partial class _Categories : System.Web.UI.Page
{
    String category = "";
    String categoryName = "";

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            category = Request.QueryString["c"];
            categoryName = Request.QueryString["n"];

            if (category == null)
            {
                Response.Redirect("~/Default.aspx");
            }
            else
            {
                var service = new ServiceUI();

                var categories = service.GetCategorys();
                Int32 icat; Int32.TryParse(category, out icat);

                //Top image
                //sectionTop.ImageUrl = "~/Images/Sections/section" + category + ".png";

                for (int i = 0; i < categories.Count(); i++)
                {
                    drpCategories.Items.Add(new ListItem(categories[i].Name, categories[i].Id.ToString()));
                    if (categories[i].Id.ToString() == category)
                    {
                        //Set the title map route

                        //lblRoute.Text = "INICIO / " + categories[i].Name.ToUpper();
                        lblRoute.Text = "INICIO / "; lblRoute.NavigateUrl = "Default.aspx";
                        lblRoute1.Text = categories[i].Name.ToUpper();
                        lblHomeHotProductsTitle.Text = categories[i].Name + " en oferta";
                        contenttitlesubcategories.InnerText = categories[i].Name;
                        //sectionTop.ImageUrl = "~/Images/Sections/section" + category + ".png";
                        sectionTop.ImageUrl = categories[i].ImageUrl;

                        contenttext.InnerHtml = categories[i].MetaDescription;

                        var subcategorieslist = new ServiceUI().GetSubcategorys((int)categories[i].Id);
                        drpCategories.Items[i + 1].Selected = true;

                        for (int j = 0; j < subcategorieslist.Count(); j++)
                        {
                            //left banner
                            var div = new HtmlGenericControl("div");
                            var hl = new HyperLink();
                            hl.ID = "sc_" + subcategorieslist[j].Id.ToString();
                            hl.Text = subcategorieslist[j].Name + " (" + subcategorieslist[j].TotalProducts + ")";
                            hl.NavigateUrl = "~/Subcategories.aspx?s=" + subcategorieslist[j].Id.ToString();
                            div.Controls.Add(hl);
                            subcategories.Controls.Add(div);

                            //content with images
                            //Main container
                            var item = new HtmlGenericControl("div");
                            if (j == 2 || j == 5 || j == 8 || j == 11 || j == 14 || j == 17)
                            {
                                item.Attributes.Add("class", "list-item-right2");
                            }
                            else
                            {
                                item.Attributes.Add("class", "list-item2");
                            }

                            //Control for link
                            var link = new HyperLink();
                            link.NavigateUrl = "~/Subcategories.aspx?s=" + subcategorieslist[j].Id;

                            //Main image
                            var img = new Image();
                            img.ImageUrl = subcategorieslist[j].ImageUrl;
                            //img.ImageUrl = "http://dataclinicc.web709.discountasp.net/arb/imagessite/notavaible.jpg";
                            img.CssClass = "list-item-image2";

                            //Product name and lower price
                            var info = new HtmlGenericControl("div");
                            info.Attributes.Add("class", "list-item-info2");

                            var infotext = new HtmlGenericControl("div");
                            infotext.Attributes.Add("class", "list-item-info-text2");

                            var infotextproductname = new Label();
                            infotextproductname.CssClass = "list-item-info-text-product2";
                            infotextproductname.Text = subcategorieslist[j].Name;

                            var infotextproductname2 = new Label();
                            infotextproductname2.CssClass = "list-item-info-text-product2";
                            infotextproductname2.Text = subcategorieslist[j].TotalProducts.ToString() + " productos";

                            int lowerPrice = (int)(subcategorieslist[j].LowerPrice == null ? 0 : subcategorieslist[j].LowerPrice);
                            var infotextlowerprice = new Label();
                            infotextlowerprice.CssClass = "list-item-info-text-lower2";
                            infotextlowerprice.Text = "Desde " + lowerPrice.ToString("C") + " pesos";

                            var cartimage = new Image();
                            cartimage.Attributes.Add("class", "list-item-text-cart2");
                            cartimage.ImageUrl = "~/Images/carrito.png";

                            infotext.Controls.Add(infotextproductname);
                            infotext.Controls.Add(infotextproductname2);
                            infotext.Controls.Add(infotextlowerprice);
                            info.Controls.Add(infotext);
                            //info.Controls.Add(cartimage);
                            info.Controls.Add(new HtmlGenericControl("br"));
                            link.Controls.Add(img);
                            link.Controls.Add(info);
                            item.Controls.Add(link);
                            contentsubcategories.Controls.Add(item);
                        }

                        contentsubcategories.Controls.Add(new HtmlGenericControl("br"));

                        var offers = new ServiceUI().GetOfertasByCategoria(Int32.Parse(category)).Take(4).ToList();
                        for (int j = 0; j < offers.Count(); j++)
                        {
                            //left banner
                            var div = new HtmlGenericControl("div");
                            var hl = new HyperLink();
                            hl.ID = "sc_" + offers[j].Id.ToString();
                            //hl.Text = offers[j].Name + " (" + offers[j].TotalProducts + ")";
                            hl.NavigateUrl = "~/Detail.aspx?d=" + offers[j].Id.ToString();
                            div.Controls.Add(hl);
                            subcategories.Controls.Add(div);

                            //contect with images
                            //Main container
                            var item = new HtmlGenericControl("div");
                            if (j == 2 || j == 5 || j == 8 || j == 11 || j == 14 || j == 17)
                            {
                                item.Attributes.Add("class", "list-item-right2");
                            }
                            else
                            {
                                item.Attributes.Add("class", "list-item2");
                            }

                            //Control for link
                            var link = new HyperLink();
                            link.NavigateUrl = "~/Detail.aspx?d=" + offers[j].Id;

                            //Main image
                            var img = new Image();
                            img.ImageUrl = offers[j].ImageUrl;
                            //img.ImageUrl = "http://dataclinicc.web709.discountasp.net/arb/imagessite/notavaible.jpg";
                            img.CssClass = "list-item-image2";

                            //Product name and lower price
                            var info = new HtmlGenericControl("div");
                            info.Attributes.Add("class", "list-item-info2");

                            var infotext = new HtmlGenericControl("div");
                            infotext.Attributes.Add("class", "list-item-info-text2");

                            var infotextproductname = new Label();
                            infotextproductname.CssClass = "list-item-info-text-product2";
                            infotextproductname.Text = offers[j].Name;

                            /*var infotextproductname2 = new Label();
                            infotextproductname2.CssClass = "list-item-info-text-product2";
                            infotextproductname2.Text = offers[j].TotalProducts.ToString() + " productos";*/

                            var infotextlowerprice = new Label();
                            infotextlowerprice.CssClass = "list-item-info-text-lower2";
                            infotextlowerprice.Text = "Desde " + offers[j].LowerPrice.ToString("C") + " pesos";

                            var cartimage = new Image();
                            cartimage.Attributes.Add("class", "list-item-text-cart2");
                            cartimage.ImageUrl = "~/Images/carrito.png";

                            infotext.Controls.Add(infotextproductname);
                            //infotext.Controls.Add(infotextproductname2);
                            infotext.Controls.Add(infotextlowerprice);
                            info.Controls.Add(infotext);
                            //info.Controls.Add(cartimage);
                            info.Controls.Add(new HtmlGenericControl("br"));
                            link.Controls.Add(img);
                            link.Controls.Add(info);
                            item.Controls.Add(link);
                            contentsubcategoriesoffer.Controls.Add(item);
                        }

                        contentsubcategoriesoffer.Controls.Add(new HtmlGenericControl("br"));
                    }
                }
            }
        }
        LoadBanners();
    }

    protected void drpCategories_SelectedIndexChanged(object sender, EventArgs e)
    {
        Response.Redirect("~/Categories.aspx?c=" + ((DropDownList)sender).SelectedValue.ToString());
    }

    protected void LoadBanners()
    {
        var banner1 = new ServiceUI().GetBanner(BannerType.Top);
        for (int i = 0; i < banner1.Count(); i++)
        {
            var c = new HyperLink() { ImageUrl = banner1[i].Url, NavigateUrl = banner1[i].LinkUrl };
            var p = new HyperLink() { NavigateUrl = banner1[i].LinkUrl, Text = ((i + 1)).ToString() };
            c.Target = "_blank";
            p.Attributes.Add("rel", ((i + 1)).ToString());
            imagereel.Controls.Add(c);
            paging.Controls.Add(p);
        }
        var banner2 = new ServiceUI().GetBanner(BannerType.Middle);
        for (int i = 0; i < banner2.Count(); i++)
        {
            var c = new HyperLink() { ImageUrl = banner2[i].Url, NavigateUrl = banner2[i].LinkUrl };
            var p = new HyperLink() { NavigateUrl = banner2[i].LinkUrl, Text = ((i + 1)).ToString() };
            c.Target = "_blank";
            p.Attributes.Add("rel", ((i + 1)).ToString());
            imagereel2.Controls.Add(c);
            paging2.Controls.Add(p);
        }
        var banner3 = new ServiceUI().GetBanner(BannerType.Bottom);
        for (int i = 0; i < banner3.Count(); i++)
        {
            var c = new HyperLink() { ImageUrl = banner3[i].Url, NavigateUrl = banner3[i].LinkUrl };
            var p = new HyperLink() { NavigateUrl = banner3[i].LinkUrl, Text = ((i + 1)).ToString() };
            c.Target = "_blank";
            p.Attributes.Add("rel", ((i + 1)).ToString());
            imagereel3.Controls.Add(c);
            paging3.Controls.Add(p);
        }
    }
}